<?php
namespace Manage\Controller;

class MemberController extends CommonController {

    private $api;
    private $user;

    protected function _initialize(){
        $login = A('Login');
        if( ! session($login::SESSION_NAME) ) $this->error('请先登录', U('Login/Index'));
        $this->api = A('Api/Request');
        $this->user = session($login::SESSION_NAME);
    }

    /**
     * 游戏列表
     */
    public function Lists(){
        $do = I('get.do');
        switch($do) {
            case 'add':
                $this->_uesrAdd();
                break;
            case 'edit':
                $this->_userEdit();
                break;
            case 'lock':
                $this->_userLock();
                break;
            case 'unlock':
                $this->_userUnlock();
                break;
            case 'log':
                $this->_userLog();
                break;
            case 'Search':
                $this->_search();
                break;
            case 'SearchFlow':
                $this->_searchFlow();
                break;
            default:
                $db = zyt_data_init();
                $total = $db->table('user')->count();
                //分页
                $nowpage = (int)I('get.page');
                $page = $nowpage > 0 ? $nowpage : 1;
                $pagesize = C('LIST_PAGE_SIZE');
                $list = $db->table('user')->order('user_updatetime desc')->page($page, $pagesize)->select();
                $this->assign('pagesize', $pagesize);
                $this->assign('pages', ceil($total/$pagesize));
                $this->assign('total', $total);
                $this->assign('list', $list);
                $this->assign('nowpages', $page);

                $this->display();
                break;
        }
    }

    private function _search(){
        $kw = trim(I('get.keyword'));
        if( !empty($kw) ){
            $db = zyt_data_init();
            $map['user_name']  = array('LIKE', "%$kw%");
            $map['user_phone'] = $kw;
            $map['user_email'] = $kw;
            $total = $db->table('user')->where($map, 'OR')->count();
            //分页
            $nowpage = (int)I('get.page');
            $page = $nowpage > 0 ? $nowpage : 1;
            $pagesize = C('LIST_PAGE_SIZE');
            $list = $db->table('user')->where($map, 'OR')->order('user_id desc')->page($page, $pagesize)->select();
            $this->assign('pagesize', $pagesize);
            $this->assign('pages', ceil($total/$pagesize));
            $this->assign('total', $total);
            $this->assign('list', $list);
            $this->assign('nowpages', $page);
            $this->display();
        }else{
            $db = zyt_data_init();
            $total = $db->table('user')->count();
            //分页
            $nowpage = (int)I('get.page');
            $page = $nowpage > 0 ? $nowpage : 1;
            $pagesize = C('LIST_PAGE_SIZE');
            $list = $db->table('user')->page($page, $pagesize);
            $this->assign('pagesize', $pagesize);
            $this->assign('pages', ceil($total/$pagesize));
            $this->assign('total', $total);
            $this->assign('list', $list);
            $this->assign('nowpages', $page);

            $this->display();
        }
    }

    private function _uesrAdd(){
        if( IS_GET ){
            $this->display('userAdd');
        }
        if( IS_POST ){
            $post = I('post.');
            $db = zyt_data_init();

            $name    = trim($post['name']);
            $pass    = trim($post['pass']);
            $paypass = trim($post['paypass']);
            $mobile  = trim($post['phone']);
            $email  = trim($post['email']);

            if( ! check_username($name) ){
                $this->error('用户名只能由4-20位英文、数字或者下划线组成');
            }
            if(  !check_userpass($pass) ){
                $this->error('请输入6-20位的密码');
            }

            if( strlen($paypass) > 0 && !check_userpass($paypass) ){
                $this->error('请输入6-20位的支付密码');
            }

            if( strlen($mobile) > 0 && ! check_mobile($mobile) ){
                $this->error('手机号码格式不正确');
            }

            if( strlen($email) > 0 && ! check_email($email) ){
                $this->error('邮箱格式不正确');
            }

            $ip = get_client_ip();
            $paypass = empty($paypass) ? '' : md5($paypass);   //传入为空时，则置支付密码为空

            $result = $db->addUser($name, md5($pass), $paypass, $mobile, $email, $ip);

            if( $result['output'][0] == 0 ){
                $this->success('添加用户成功');
            }elseif( $result['output'][0] == 1 ){
                $this->error('用户名已被占用');
            }elseif( $result['output'][0] == 2 ){
                $this->error('手机号码已被占用');
            }elseif( $result['output'][0] == 3 ){
                $this->error('邮箱已被占用');
            }else{
                $this->error('新增用户失败，请重试');
            }
        }
    }

    private function _userEdit(){
        $id= I ('get.id');
        $db = zyt_data_init();
        if( $id ){
            $this->mark = $db->table('user')->where("user_id=$id")->find();
            $this->display('userAdd');
        }

        if( IS_POST ){
            $post = I('post.');

            $id      = trim($post['id']);
            $pass    = trim($post['pass']);
            $paypass = trim($post['paypass']);
            $mobile  = trim($post['phone']);
            $email   = trim($post['email']);

            if( strlen($pass) > 0 && !check_userpass($pass) ){
                $this->error('请输入6-20位的密码');
            }elseif(strlen($pass) == 0){
                $pass = NULL;
            }

            if( strlen($paypass) > 0 && !check_userpass($paypass) ){
                $this->error('请输入6-20位的支付密码');
            }elseif(strlen($paypass) == 0){
                $paypass = NULL;
            }

            if( strlen($mobile) > 0 && ! check_mobile($mobile) ){
                $this->error('手机号码格式不正确');
            }elseif(strlen($mobile) == 0){
                $mobile = '';
            }

            if( strlen($email) > 0 && ! check_email($email) ){
                $this->error('邮箱格式不正确');
            }elseif(strlen($email) == 0){
                $email = '';
            }

            $pass    = NULL === $pass    ? NULL : md5($pass);
            $paypass = NULL === $paypass ? NULL : md5($paypass);

            $result = $db->editUser($id, $pass, $paypass, $mobile, $email);
            if( $result['output'][0] == 0 ){
                $this->success('修改用户成功');
            }elseif( $result['output'][0] == 1 ){
                $this->error('手机号码已被占用');
            }elseif( $result['output'][0] == 2 ){
                $this->error('邮箱已被占用');
            }else{
                $this->error('修改用户失败，请重试');
            }
        }
    }
    private function _userLock(){
        if( IS_GET ){
            $id= I ('get.id');
            $page=I('get.page');
            $db = zyt_data_init();
            $data['user_is_disable'] = 1;
            $mark = $db->table('user')->where("user_id=$id")->save($data);
            if($mark){
                $this->success('已禁用','/Member/Lists?page='.$page);
            }else{
                $this->error('操作失败，请重试');
            }
        }
    }
    private function _userUnlock(){
        if( IS_GET ){
            $id= I ('get.id');
            $page=I('get.page');
            $db = zyt_data_init();
            $data['user_is_disable'] = 0;
            $mark = $db->table('user')->where("user_id=$id")->save($data);
            if($mark){
                $this->success('已启用','/Member/Lists?page='.$page);
            }else{
                $this->error('操作失败，请重试');
            }
        }
    }

    private function _userLog(){
        if( IS_GET ){
            $id= I ('get.id');
            $db = zyt_data_init();
            $map['money_log_master_user_id'] = $id;

            $total = $db->table('money_log as A')
                ->where($map)
                ->join('platform_info as B', 'A.money_log_platform_id', 'B.platform_id')
                ->join('sso_common_class as C on A.money_log_class = C.common_class_id')
                ->order('money_log_time desc')
                ->count();

            $trade_type = $db->table('common_class')->field('common_class_id, common_class_name')->where("common_class_sort = 'LB0001'")->select();
            $this->assign('trade_type', $trade_type);

            //分页
            $nowpage = (int)I('get.page');
            $page = $nowpage > 0 ? $nowpage : 1;
            $pagesize = C('LIST_PAGE_SIZE');

            $list = $db->table('money_log as A')
                ->field('A.*, B.platform_name, C.common_class_name')
                ->where($map)
                ->join('platform_info as B', 'A.money_log_platform_id', 'B.platform_id')
                ->join('sso_common_class as C on A.money_log_class = C.common_class_id')
                ->order('money_log_time desc')
                ->page($page, $pagesize)
                ->select();

            $this->assign('pagesize', $pagesize);
            $this->assign('pages', ceil($total/$pagesize));
            $this->assign('total', $total);
            $this->assign('list', $list);

            $this->assign('id',$id);
            $this->display('userLog');
        }
    }

    private function _searchFlow(){
        if( IS_GET ){
            $get  = I('get.');
            $id   = trim($get['id']);
            $type = trim($get['trade_type']);
            $date = trim($get['date']);
            $sn   = trim($get['order_sn']);

            $db = zyt_data_init();
            $trade_type = $db->table('common_class')->field('common_class_id, common_class_name')->where("common_class_sort = 'LB0001'")->select();
            $this->assign('trade_type', $trade_type);

            $where = "money_log_master_user_id = $id";

            if( $type > 0 ){
                $where .= " AND money_log_class = $type";
            }

            if(strpos($date, '~')){
                $tmp = explode('~', $date);
                $start_date = trim($tmp[0]);
                $end_date   = trim($tmp[1]);
                $start = strtotime($start_date);
                $end   = strtotime($end_date) + 86400;  //end需要加一天，防止当天的数据搜索不出来
                $where .= " AND money_log_time BETWEEN $start AND $end";
            }

            if( strlen($sn) > 0 ){
                $where .= " AND money_log_no = '$sn'";
            }

            $total = $db->table('money_log as A')
                ->where($where)
                ->join('platform_info as B', 'A.money_log_platform_id', 'B.platform_id')
                ->join('sso_common_class as C on A.money_log_class = C.common_class_id')
                ->order('money_log_time desc')
                ->count();

            //分页
            $nowpage = (int)I('get.page');
            $page = $nowpage > 0 ? $nowpage : 1;
            $pagesize = C('LIST_PAGE_SIZE');

            $list = $db->table('money_log as A')
                ->field('A.*, B.platform_name, C.common_class_name')
                ->where($where)
                ->join('platform_info as B', 'A.money_log_platform_id', 'B.platform_id')
                ->join('sso_common_class as C on A.money_log_class = C.common_class_id')
                ->order('money_log_time desc')
                ->page($page, $pagesize)
                ->select();

            $this->assign('pagesize', $pagesize);
            $this->assign('pages', ceil($total/$pagesize));
            $this->assign('total', $total);
            $this->assign('list', $list);

            $this->assign('type', $type);
            $this->assign('start', $start_date);
            $this->assign('end', $end_date);
            $this->assign('order_sn', $sn);

            $this->assign('id',$id);
            $this->display('userLog');
        }
    }
}